var BattleField = function()
{             
    this.ship = new Ship(); 
    for ( var i = 0; i < this.ship.numLazer; i++ )
    {
        this.ship.lazerGun[i] = new Lazer();
    }                      
    this.stars = [];
    this.asteroids = [];     
    this.meteorits = [];
    this.width = 0;
    this.height = 0;
    this.lifeBonus = new LifeBonus( this.width, this.height );                                             
};

BattleField.prototype.start = function()
{
    var canvas = document.getElementById( 'canvas' );
    var context = canvas.getContext( '2d' );
    this.width = canvas.width;
    this.height = canvas.height;

    var thisPtr = this;
    var animationHandler = function()
    {
        window.requestAnimationFrame( animationHandler, canvas );
        thisPtr.drawFrame( context );
    };
    var keyDownHandler = function( event )
    {
        thisPtr.keyDownHandler( event );
    };
    window.requestAnimationFrame( animationHandler, canvas );
    window.addEventListener( 'keydown', keyDownHandler, false );
};

BattleField.prototype.drawFrame = function( context )
{                             
    this.drawStars( context );
    this.drawMeteorits( context );
    this.drawLazer( context );
    this.drawShip( context );
    this.drawAsteroids( context );
    this.drawBonus( context );
    this.isCollisionAsteroidAndShip( context );
    this.isCollisionAsteroidsAndLazers( context );
    this.isCollisionShipAndBonus();
    this.drawBars( context );
};

BattleField.prototype.keyDownHandler = function( event )
{
    event.preventDefault();
    switch( event.keyCode )
    {
      case keycode.UP:                                
        this.ship.y -= this.ship.speed;
        break;
      case keycode.DOWN:                              
        this.ship.y += this.ship.speed;
        break;
      case keycode.LEFT:                              
        this.ship.x -= this.ship.speed;                    
        break;
      case keycode.RIGHT:                             
        this.ship.x += this.ship.speed;                
        break;
      case keycode.SPACE:    
        this.makeLazerShot();            
        break;
      default:
        break;    
    }                              
};
             
BattleField.prototype.drawBonus = function( context )
{                           
    this.lifeBonus = getLifeBonus( this.lifeBonus, this.width, this.height );

    if ( this.lifeBonus.life == 'true' )
    {   
        this.lifeBonus.draw( context );
    }
}

BattleField.prototype.isCollisionShipAndBonus = function()
{
    var collision = 'false';
    var j = 0;             

    collision = intersectionSquares( 
                    this.ship.x,
                    this.ship.y,
                    this.ship.shipWidth,
                    this.ship.shipHeight,
                    this.lifeBonus.x,
                    this.lifeBonus.y,
                    this.lifeBonus.width,
                    this.lifeBonus.height
                );
    if ( collision == 'true' )
    {
        if ( this.ship.life < maxLifeShip )
        {
            this.ship.life++;
            this.lifeBonus.life = 'false';
        }
    }
}

BattleField.prototype.makeLazerShot = function()
{    
    var numLazerInFirstShot = numGun;       
    if ( isFreeGun( this.ship ) == 'true' )
    {                                 
        for ( var i = 0; i < this.ship.numLazer; i++ )
        {
            if ( ( numLazerInFirstShot > 0 ) && ( this.ship.lazerGun[i].life == 'false' ) )
            {
                this.ship.lazerGun[i].life = 'true';
                this.ship.lazerGun[i].y = this.ship.y;
                this.ship.lazerGun[i].x = ( numLazerInFirstShot == 2 ) ? this.ship.x + positionXLeftGun : this.ship.x + positionXRightGun; 
                numLazerInFirstShot--;       
                this.ship.gasShotEffect = 'true';
                this.ship.shot.play();
            }
        }
    }    
}

BattleField.prototype.drawMeteorits = function( context )
{              
    this.meteorits = getMeteorits( numMeteorits, this.meteorits, this.width, this.height );
    for ( var i = 0; i < numMeteorits; i++ )
    {                    
        if ( this.meteorits[i].life == 'true' )
        {
            this.meteorits[i].draw( context );
        }
    }
}

BattleField.prototype.drawShip = function( context )
{   
    redeliver = effectShipDied( this.ship.effectTime );
    this.ship.effectTime = redeliver.effectTime;
    
    if ( redeliver.effect == 'false' )
    {
        this.ship.draw( context, this );
    }   
};                    

BattleField.prototype.drawStars = function( context )
{
    context.clearRect( 0, 0, this.width, this.height );
    this.stars = getStarsSky( numStars, this.stars, this.width, this.height );
    for ( var i = 0; i < numStars; i++ )
    {    
        this.stars[i].draw( context );
    }
};   
                  
BattleField.prototype.drawAsteroids = function( context )
{             
    this.asteroids = getAsteroids( numAsteroids, this.asteroids, this.width, this.height );  
    for ( var i = 0; i < numAsteroids; i++ )
    {
        if ( this.asteroids[i].life == 'true' )
        {
            this.asteroids[i].draw( context );
        }       
    }
};  

BattleField.prototype.isCollisionAsteroidAndShip = function( context )
{
    var collision = 'false';
    
    for ( var j = 0; j < this.asteroids.length; j++)
    {                  
        if ( this.asteroids[j].life == 'true' )
        {
            collision = intersectionSquares( 
                            this.ship.x,
                            this.ship.y,
                            this.ship.shipWidth,
                            this.ship.shipHeight,
                            this.asteroids[j].x,
                            this.asteroids[j].y,
                            this.asteroids[j].asteroidWidth,
                            this.asteroids[j].asteroidHeight
                        );
            if ( collision == 'true' )
            {   
                if ( this.ship.effectTime == 0 )
                {
                    this.ship.life -= 1;
                }
                this.asteroids[j].life = 'false';
                this.ship.numDestroyedAsteroids += 1;
                this.ship.score += pointHitShipAndAsteroid;
                this.ship.effectTime = 6;
                makeAsteroidBangEffect( this.asteroids[j], context );
            }
        }
    }                                                                                                                                                                         
}

BattleField.prototype.drawBars = function( context )
{
    barsDraw( context, this );
}                             

BattleField.prototype.isCollisionAsteroidsAndLazers = function( context )
{                 
    for ( var i = 0; i < this.ship.numLazer; i++ )
    {
        var collision = 'false';   

        for ( var j = 0; this.ship.lazerGun[i].life == 'true' && j < this.asteroids.length; j++ )
        {                      
            if ( this.asteroids[j].life == 'true' )
            {
                collision = intersectionSquares(
                                this.asteroids[j].x,
                                this.asteroids[j].y,
                                this.asteroids[j].asteroidWidth,
                                this.asteroids[j].asteroidHeight,
                                this.ship.lazerGun[i].x,
                                this.ship.lazerGun[i].y,
                                this.ship.lazerGun[i].width,
                                this.ship.lazerGun[i].height
                            );
                if ( collision == 'true' )
                {    
                    this.asteroids[j].hp -= this.ship.lazerGun[i].hp;
                    if ( this.asteroids[j].hp <= 0 )
                    {
                        this.asteroids[j].life = 'false';
                        this.ship.numDestroyedAsteroids += 1;
                        makeAsteroidBangEffect( this.asteroids[j], context );
                    }                                    
                    this.ship.score += pointOneHitLazer;
                    this.ship.lazerGun[i].life = 'false';
                }
            } 
        }
    }
}

BattleField.prototype.drawLazer = function( context )
{                         
    this.ship.lazerGun = getLazerGun( this.ship.numLazer, this.ship.lazerGun );
                                                                               
    for ( var i = 0; i < this.ship.numLazer; i++ )
    {
        if ( this.ship.lazerGun[i].life == 'true' )
        {                                                          
            this.ship.lazerGun[i].draw( context );
        }
    }            
}

var onloadHandler = function()
{
    var battleField = new BattleField();
    
    battleField.start();
}
window.onload = onloadHandler;